package hhm.android.my

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.library.calendar.DateUtil
import hhm.android.library.utils.GlideUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.SBApplication
import siau.android.base.getSureDialog
import siau.android.http.model.ChildModel
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.abs

class MyBabyListAdapter(var context: Context, list: ArrayList<ChildModel>, var listener: OnMultiClickListener, var deleteListener: OnMultiClickListener) : HBaseAdapter<MyBabyListViewHolder, ChildModel>(list) {
    override fun bindView(parent: ViewGroup, viewType: Int): MyBabyListViewHolder {
        val v = LayoutInflater.from(context).inflate(R.layout.item_baby_list, parent, false)
        return MyBabyListViewHolder(v)
    }

    override fun bindData(holder: MyBabyListViewHolder, bean: ChildModel, position: Int) {
        holder.name.text = bean.nickName
        val s = DateUtil.getMonthDiff(Date(bean.birthday), Date())
        val age = if (s > 35) {
            "${(s / 12)}岁"
        } else {
            "${s}个月"
        }
        holder.age.text = age
        if (bean.sex == 2) {//女
            if (bean.avatar.isNullOrEmpty()) {
                holder.icon.setImageResource(R.mipmap.baby_girl)
            } else {
                GlideUtils.loadCircleImage(context, bean.avatar, holder.icon, R.mipmap.baby_girl, R.mipmap.baby_girl)
            }
        } else {
            if (bean.avatar.isNullOrEmpty()) {
                holder.icon.setImageResource(R.mipmap.baby_boy)
            } else {
                GlideUtils.loadCircleImage(context, bean.avatar, holder.icon, R.mipmap.baby_boy, R.mipmap.baby_boy)
            }
        }

        holder.delete.setOnClickListener {
            if (SBApplication.userData.isCreator == 1) {
                holder.sml.quickClose()
                getSureDialog(
                    context, { i ->
                        i.tag = bean
                        deleteListener.onMultiClick(i)
                    }, content = context.getString(R.string.sure_delete_baby), showTitle = false
                )
            } else {
                Toast.makeText(context, context.getString(R.string.you_cannot_delete_baby), Toast.LENGTH_SHORT).show()
            }
        }
        holder.content.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                v?.tag = bean
                listener.onMultiClick(v)
            }
        })
    }
}